#include "delay.h"

static u8  fac_us=0;
static u16 fac_ms=0;

void delay_init(u8 SYSCLK)
{
    SysTick->CTRL&=0xfffffffb;
    fac_us=SYSCLK/8;
    fac_ms=(u16)fac_us*1000;
}
void delay_ms(u16 nms)
{
    SysTick->LOAD=(u32)nms*fac_ms;
    SysTick->CTRL|=0x01;
    while(!(SysTick->CTRL&(1<<16)));
    SysTick->CTRL&=0XFFFFFFFE;
    SysTick->VAL=0X00000000;
}

void delay_us(u32 Nus)
{
    SysTick->LOAD=Nus*fac_us;
    SysTick->CTRL|=0x01;
    while(!(SysTick->CTRL&(1<<16)));
    SysTick->CTRL=0X00000000;
    SysTick->VAL=0X00000000;
}

void inline delay_ns(u32 nns)
{
	SysTick->LOAD=nns;
    SysTick->CTRL|=0x01;
    while(!(SysTick->CTRL&(1<<16)));
    SysTick->CTRL=0X00000000;
    SysTick->VAL=0X00000000;
}
